//
//  MyOrderView.m
//  X-Touch 2.0
//
//  Created by shengchao yang on 12-2-23.
//  Copyright (c) 2012年 home user. All rights reserved.
//

#import "MyOrderView.h"
#import "CookSelectView.h"
#import "MessageBoxBaseView.h"
#import "FullScreenMode.h"
#import "PopoverLanguageSelect.h"
#import "PopoverTableViewController.h"
#import "PopoverOtherFunctions.h"
#import "Singleton.h"
#import "Common.h"
#import "NSString+Additions.h"
#import "FileManager.h"

@interface MyOrderView(Private)
-(void) setBtnsEnable;
-(void) changeNumberCookNameAndPrice:(NSInteger)type;
@end

@implementation MyOrderView
@synthesize myOrderTableView;
@synthesize myOrderNineView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}
- (IBAction)buttonClicked:(id)sender {
    if (AppSession.isAnimating) {
        MyNSLog(@"上一个动画还未结束...");
        return;
    }
    [AppSession playMusic:VoiceFile(@"clicked.caf")];
    NSInteger bid = [sender tag];
    UIButton *btn = (UIButton *)[[self.view viewWithTag:TagOfUIView] viewWithTag:bid];
    if (bid == TagOfUIButton) {//return
        MyNSLog(@"继续点菜");
        AppSession.isCommendView = NO;
        NSDictionary *infos = [NSDictionary dictionaryWithObjectsAndKeys:@"MyOrderView", @"FromNibName", @"GuestOrderingView",@"ToNibName",nil];
        postNWithInfos(SysMsg_RootView_ControllerChange, self, infos);
    }else if(bid == TagOfUIButton + 1){//pic
        MyNSLog(@"图片模式");
        postN(SysMsg_MyOrderView_removeTableListIntoNineMode);
        postN(@"SysMsg_MyOrderNineModeView_refreshNineMode");
    }else if(bid == TagOfUIButton + 2){//list
        MyNSLog(@"列表模式");
        postN(SysMsg_MyOrderView_removeNineModeIntoTableList);
        postN(@"SysMsg_MyOrderTableModeView_refreshMyOrderTable");
    }else if(bid == TagOfUIButton + 4){//数量        
        MyNSLog(@"改数量");       
        [self changeNumberCookNameAndPrice:1];
    }else if(bid == TagOfUIButton + 5){//烧法        
        MyNSLog(@"改烧法");       
        [self changeNumberCookNameAndPrice:2];
    }else if(bid == TagOfUIButton + 6){//菜名
        MyNSLog(@"改菜名");
        [self changeNumberCookNameAndPrice:3];
    }else if(bid == TagOfUIButton + 7){//单价
        MyNSLog(@"改单价");
        [self changeNumberCookNameAndPrice:4];
    }else if(bid == TagOfUIButton + 8){//language
        MyNSLog(@"语言选择");
        PopoverLanguageSelect *pls = [[PopoverLanguageSelect alloc] initWithNibName:@"PopoverLanguageSelect" bundle:nil];
        [pls PopOverFromRect:btn.frame inView:[self.view viewWithTag:TagOfUIView] directions:UIPopoverArrowDirectionUp];
        [pls release];
    }else if(bid == TagOfUIButton + 10){//more
        MyNSLog(@"更多功能");        
        //版本二
        PopoverOtherFunctions *moof = [[PopoverOtherFunctions alloc] initWithNibName:@"PopoverOtherFunctions" bundle:nil];
        [moof PopOverFromRect:btn.frame inView:[self.view viewWithTag:TagOfUIView] directions:UIPopoverArrowDirectionUp];
        [moof release];  
    }
}
//修改菜品四个属性
-(void) changeNumberCookNameAndPrice:(NSInteger)type{
    if ([AppSession.currentOrderMenu.momSendErr isEqualToString:@"1"]) {
        postN(@"SysMsg_RootView_AlertErrMsg");
        return;
    }
    BOOL updateAble = YES;
    NSString * errMsg = @"";
    if (AppSession.currentMyOrderDish == nil) {
        errMsg = TranslateStr(@"没选中菜品") ;
        updateAble = NO;
    }else{
        if (type == 3 || type == 4) {
            if (![AppSession.currentMyOrderDish.modPluPlu.ppTemp isEqualToString:@"1"]) {
                if (type == 3) {//菜名
                    errMsg = TranslateStr(@"不能改菜名");
                }else if(type == 4){//单价
                    errMsg = TranslateStr(@"不能改单价"); 
                }
                updateAble = NO;
            }
        }
    }

    if (!updateAble) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TranslateStr(@"提示")
                                                        message:errMsg
                                                       delegate:nil 
                                              cancelButtonTitle:TranslateStr(@"确定")
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }else {
        if (type == 2) {
            NSDictionary *infos = [NSDictionary dictionaryWithObjectsAndKeys:AppSession.currentMyOrderDish, @"pluObj",nil];
            postNWithInfos(@"SysMsg_RootView_PopOutSelectCookWithInfo", nil, infos);
        }else {
            alertType = type;
            UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
            [myTextField setBackgroundColor:[UIColor whiteColor]];
            [myTextField setTag:TagOfUITextField];
            
            NSString * titleName = TranslateStr(@"改数量");
            myTextField.text = AppSession.currentMyOrderDish.modNumber;
            if (type == 3) {
                titleName = TranslateStr(@"改菜名");
                myTextField.text = JsonField(AppSession.currentMyOrderDish.modDescripts);
            }else if(type == 4){
                titleName = TranslateStr(@"改单价");
                myTextField.text = AppSession.currentMyOrderDish.modPrice;
            }
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:titleName
                                                            message:@"this gets covered1" 
                                                           delegate:self 
                                                  cancelButtonTitle:TranslateStr(@"取消") 
                                                  otherButtonTitles:TranslateStr(@"确定"), nil];
            [alert addSubview:myTextField];
            [myTextField release];  
            [alert show];
            [alert release];
        }
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==alertView.cancelButtonIndex) {
        MyNSLog(@"点击了取消按钮");
    }else{
        UITextField *myTextField = (UITextField *)[alertView viewWithTag:TagOfUITextField];
        //合法性判断
        
        if (alertType == 1) {//number
            NSString * newNum = [myTextField.text replaceFrom:@"-" To:@""];//不能点负数            
            [AppSession.currentMyOrderDish updateNumber:[NSString stringWithFormat:@"%0.2f", [newNum floatValue]]];
        }else if(alertType == 3) {//name
            NSString * oldName = JsonField(AppSession.currentMyOrderDish.modDescripts);
            NSString * newName = Trim(myTextField.text);
            [AppSession.currentMyOrderDish updateName:[AppSession.currentMyOrderDish.modDescripts replaceFrom:oldName To:newName]];
        }else if(alertType == 4){//price
            [AppSession.currentMyOrderDish updatePrice:[NSString stringWithFormat:@"%0.2f", [myTextField.text floatValue]]];
        }
        postN(@"SysMsg_MyOrderTableModeView_refreshMyOrderTable");//刷新界面
    }
}
//切换到九宫格的图片模式
-(void) removeTableListIntoNineMode{
    [myOrderTableView removeFromSuperview];
    [self.view addSubview:myOrderNineView];
    isTableList = NO;
    [self setBtnsEnable];
}
//切换到列表模式
-(void) removeNineModeIntoTableList{
    [myOrderNineView removeFromSuperview];
    [self.view addSubview:myOrderTableView];
    isTableList = YES;
    [self setBtnsEnable];
}
//设置按钮是否可用
-(void) setBtnsEnable{
    btnPic.highlighted = btnNumber.highlighted = btnCook.highlighted = btnName.highlighted = btnPrice.highlighted = !isTableList;
    btnPic.enabled = btnNumber.enabled = btnCook.enabled = btnName.enabled = btnPrice.enabled = isTableList;
    btnList.highlighted = isTableList;
    btnList.enabled = !isTableList;
}
//刷新Toolbar
-(void) refreshToolBar{
    [(UILabel *)[self.view viewWithTag:TagOfUILabel+0] setText:TranslateStr(AppSession.currentOrderMenu.ptTableNo)];
    [(UILabel *)[self.view viewWithTag:TagOfUILabel+1] setText:AppSession.currentOrderMenu.persons];
    [(UILabel *)[self.view viewWithTag:TagOfUILabel+2] setText:TranslateStr(AppSession.currentOrderMenu.peEmpno)];
    
    //翻译
    [(UILabel *)[self.view viewWithTag:TagOfUILabel+3] setText:[NSString stringWithFormat:@"%@：",TranslateStr(@"桌号")]];
    [(UILabel *)[self.view viewWithTag:TagOfUILabel+4] setText:[NSString stringWithFormat:@"%@：",TranslateStr(@"人数")]];
    [(UILabel *)[self.view viewWithTag:TagOfUILabel+5] setText:[NSString stringWithFormat:@"%@：",TranslateStr(@"工号")]];
    
    [btnReturn setTitle:TranslateStr(@"点菜") forState:UIControlStateNormal];
    [btnNumber setTitle:TranslateStr(@"数量") forState:UIControlStateNormal];
    [btnCook setTitle:TranslateStr(@"烧法") forState:UIControlStateNormal];
    [btnName setTitle:TranslateStr(@"菜名") forState:UIControlStateNormal];
    [btnPrice setTitle:TranslateStr(@"单价") forState:UIControlStateNormal];
    [btnLang setTitle:TranslateStr(@"语言") forState:UIControlStateNormal];
    [btnMore setTitle:TranslateStr(@"更多功能") forState:UIControlStateNormal];
}
#pragma mark - View lifecycle
//-(void)loadView{MyNSLog(@"loadView");}//为什么会不断调用而界面不显示？？？
-(void)viewWillAppear:(BOOL)animated{MyNSLog(@"viewWillAppear");}
-(void)viewWillDisappear:(BOOL)animated{MyNSLog(@"viewWillDisappear");}
-(void)viewWillLayoutSubviews{MyNSLog(@"viewWillLayoutSubviews");}
-(void)viewWillUnload{MyNSLog(@"viewWillUnload");}

- (void)viewDidLoad{    
    ////////////////////初始化数据 begin////////////////////
    AppSession.isSortedByMenu = YES;
//    [AppSession initMyOrderDishes];//这里没必要初始化九宫格的数组
    [AppSession initMyOrderSortedDishes]; //我的点单初始化默认是列表模式
    btnReturn = (UIButton *)[[self.view viewWithTag:TagOfUIView] viewWithTag:TagOfUIButton + 0];
    btnPic = (UIButton *)[[self.view viewWithTag:TagOfUIView] viewWithTag:TagOfUIButton + 1];
    btnList = (UIButton *)[[self.view viewWithTag:TagOfUIView] viewWithTag:TagOfUIButton + 2];
    btnNumber = (UIButton *)[[self.view viewWithTag:TagOfUIView] viewWithTag:TagOfUIButton + 4];
    btnCook = (UIButton *)[[self.view viewWithTag:TagOfUIView] viewWithTag:TagOfUIButton + 5];
    btnName = (UIButton *)[[self.view viewWithTag:TagOfUIView] viewWithTag:TagOfUIButton + 6];
    btnPrice = (UIButton *)[[self.view viewWithTag:TagOfUIView] viewWithTag:TagOfUIButton + 7];
    btnLang = (UIButton *)[[self.view viewWithTag:TagOfUIView] viewWithTag:TagOfUIButton + 8];
    btnMore = (UIButton *)[[self.view viewWithTag:TagOfUIView] viewWithTag:TagOfUIButton + 10];
    ////////////////////初始化数据 end//////////////////////
    addNObserver(@selector(removeTableListIntoNineMode),SysMsg_MyOrderView_removeTableListIntoNineMode);
    addNObserver(@selector(removeNineModeIntoTableList),SysMsg_MyOrderView_removeNineModeIntoTableList);
    addNObserver(@selector(refreshToolBar), @"SysMsg_MyOrderView_refreshToolBar");
    postN(@"SysMsg_MyOrderView_refreshToolBar");
    postN(SysMsg_MyOrderView_removeNineModeIntoTableList);
    [super viewDidLoad];
}

- (void)viewDidUnload{
    [self setMyOrderTableView:nil];
    [self setMyOrderNineView:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
	return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ;
}
- (void)dealloc {
    MyNSLog(@"MyOrderView is deallocing");
    removeNObserver(SysMsg_MyOrderView_removeTableListIntoNineMode);
    removeNObserver(SysMsg_MyOrderView_removeNineModeIntoTableList);
    removeNObserver(@"SysMsg_MyOrderView_refreshToolBar");
    [myOrderTableView release];
    [myOrderNineView release];
    [super dealloc];
}
@end
